import { _decorator, Component, Node } from "cc"
import { util } from "../util/util"
import { GlobalData } from "../GlobalData"
import { ItemBase } from "./itemBase"
import { UITransform } from "cc"
import { constant } from "../framework/constant"
const { ccclass, property } = _decorator

@ccclass("Bullet")
export class Bullet extends Component {
  public bulletId: number = 0

  public currAttack: number = 1

  protected speed: number = 10

  public isEnemy: boolean = false

  private isBoss: boolean = false

  protected onEnable(): void {}

  protected onDisable(): void {
    this.unscheduleAllCallbacks()
  }

  public initTarget(bulletId: number, attack: number, isEnemy: boolean,isBoss:boolean = false) {
    this.bulletId = bulletId
    this.isEnemy = isEnemy
    this.currAttack = attack //+ Math.round((Math.random() - 0.5) * attack * 0.2)
    this.speed = constant.BULLET_SPEED
    this.isBoss = isBoss
  }

  protected numDestroy: number = 0
  update(deltaTime: number) {
    if (!GlobalData.instance.getGameState()) return

    const distance = this.speed * deltaTime * 60
    const angleInRadians = this.node.angle * (Math.PI / 180) // 将角度转换为弧度
    const dx = Math.cos(angleInRadians) * distance // 计算 x 方向的移动
    const dy = Math.sin(angleInRadians) * distance // 计算 y 方向的移动
    this.node.positionX += dx
    this.node.positionY += dy

    const arrDisBullet: number[] = [10, 40, 10, 40, 20, 50]
    const disBullet: number = arrDisBullet[this.bulletId - 1]

    if (!this.isEnemy) {
      for (let i: number = 0; i < GlobalData.instance.arrNodeMonster.length; i++) {
        let node = GlobalData.instance.arrNodeMonster[i]
        if (node && node.isValid) {
          const maxCollisionDis: number = 400
          const posY: number = this.node.positionY
          const dis = util.getTwoPosXYLength(node.positionX, node.positionY, this.node.positionX, posY)
          if (dis < maxCollisionDis) {
            const item: ItemBase = node.getComponent(ItemBase)
            if (item && !item.isDie) {
              if (dis < item.beAttackScope + disBullet) {
                const attack = this.currAttack + GlobalData.instance.getCurrPlayerAttack()
                item.onBeAttack(attack)
                GlobalData.instance.arrRoleDamage[this.bulletId - 1] += attack
                this.onDestroySelf()
                break
              }
            }
          }
        }
      }
    } else {
      // return
      for (let i: number = 0; i < GlobalData.instance.arrNodePlayerRole.length; i++) {
        let node = GlobalData.instance.arrNodePlayerRole[i]
        if (node && node.isValid) {
          const maxCollisionDis: number = 400
          const posY: number = this.node.positionY
          const dis = util.getTwoPosXYLength(node.positionX, node.positionY, this.node.positionX, posY)
          if (dis < maxCollisionDis) {
            const item: ItemBase = node.getComponent(ItemBase)
            if (item && !item.isDie) {
              if (dis < item.beAttackScope + disBullet) {
                let currAttack = this.currAttack
                if(this.isBoss){
                  currAttack = item.maxHp * 0.5
                }
                item.onBeAttack(currAttack)
                this.onDestroySelf()
                break
              }
            }
          }
        }
      }
    }

    this.numDestroy += deltaTime
    if (this.numDestroy > 4) {
      this.onDestroySelf()
    }
  }

  onDestroySelf() {
    this.node.destroy()
    this.destroy()
  }
}
